<%@ page isELIgnored="false" %><%--isELIgnored=true禁用EL表达式--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- JSTL标签库是一个JSP标签的集合，封装了许多jsp应用程序通用的核心功能-->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/layui-v2.6.13/layui/css/layui.css"/>
    <script src="${pageContext.request.contextPath}/js/jquery.min-2.1.1.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/layui-v2.6.13/layui/layui.js"></script>
    <style>
        .layui-form{
            margin-top: 300px;
            margin-left: 300px;
        }
        #d1{
            margin-left: 300px;
        }
    </style>
</head>
<body>
    <form class="layui-form" action="">
        <div class="layui-form-item" style="text-align: center;">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-inline">
                <input type="text" name="gName" class="layui-input">
            </div>
            <label class="layui-form-label">上传图片</label>
            <div class="layui-input-inline">
                <button type="button" class="layui-btn" id="imgUpload">
                    <input type="hidden" id="imgUrl" name="imgUrl" class="layui-input" value="">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                </button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">库存</label>
            <div class="layui-input-inline">
                <input type="text" name="gCount" class="layui-input">
            </div>
            <label class="layui-form-label">单价</label>
            <div class="layui-input-inline">
                <input type="text" name="gPrice" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">一级分类</label>
            <div class="layui-input-inline">
                <select name="" lay-filter="f1">
                    <option value="">一级分类</option>
                    <c:forEach items="${first}" var="first">
                        <option
<%--                                <c:if test="${parentId == first.id}"> selected=true </c:if>--%>
                                value="${first.id}">${first.sName}</option>
                    </c:forEach>
                </select>
            </div>
            <label class="layui-form-label">二级分类</label>
            <div class="layui-input-inline">
                <select name="sId" id="second">

                </select>
            </div>
        </div>
    </form>
    <div class="layui-form-item" id="d1">
        <div class="layui-input-block">
            <button class="layui-btn" style="width: 500px;"onclick="add()">添加</button>
        </div>
    </div>

</body>
<script>
    layui.use('upload', function(){
        var upload = layui.upload;
        //执行实例
        upload.render({
            elem: '#imgUpload' //绑定元素
            ,url: 'upload' //上传接口
            ,accept: 'images' //指定允许上传时校验的文件类型，可选值有：images（图片）、file（所有文件）、video（视频）、audio（音频）
            ,acceptMime: 'image/*' //规定打开文件选择框时，筛选出的文件类型，值为用逗号隔开的 MIME 类型列表
            ,done: function(res){//上传完毕回调,res必须是json格式
                console.info(res);
                console.info(res.imgUrl);
                var url = res.imgUrl;
                var imgUrl = new Array();
                imgUrl = url.split('/');
                console.info(imgUrl)

                // $("#i1").attr("value","1");
                $("#imgUrl").attr("value","http://127.0.0.1:8887/"+imgUrl[2])  ;
                console.info($("#imgUrl").val())
                if (res.code == 1){
                    alert("上传成功")
                }else {
                    alert("上传失败")
                }
            }
            ,error: function(){
                //请求异常回调
            }
        });
    });

    function add(){
        var params = $("form").serialize();

        console.info(params);
        $.ajax({
            url: "http://localhost:8080/shopping/goods/addOneGoods",
            data: params,
            type: "post",
            success: function(code){
                if(code == '1'){
                    alert("添加成功");
                    window.location.href="http://localhost:8080/shopping/goods/queryUpGoods"
                } else if (code == '0') {
                    alert("添加失败");
                }else if (code == '2'){
                    alert("不能添加空商品")
                }
                //延迟关闭
                // setTimeout(closeLayer(), 500);
            }
        });
    }
    // function closeLayer(){
    //     //关闭弹出层
    //     var index = parent.layer.getFrameIndex(window.name);
    //     parent.layer.close(index);
    //     //刷新父页面
    //     window.parent.location.reload();
    // }
    layui.use(['layer','form'], function(){
        var layer = layui.layer;
        var form = layui.form;
        form.on('select(f1)', function (data) {
            console.info(data.elem); //得到select原始DOM对象
            console.info(data.value); //得到被选中的值

            var params = "parentId=" + data.value;
            $.ajax({
                url: "http://localhost:8080/shopping/goods/querySecond",
                data: params,
                type: "post",
                success: function (obj) {
                    //将json字符串转为数组
                    var arr = JSON.parse(obj);

                    //清空子元素
                    $("#second").empty();
                    $("#second").append("<option value=''>二级分类</option>");
                    //添加子元素
                    $.each(arr, function (index, item) {
                        console.info(item);
                        //在元素内部末尾插入子元素
                        $("#second").append(
                            "<option value='" + item.id + "'>" + item.name
                            + "</option>");
                    });
                    //重新渲染select下拉框
                    form.render('select');
                }
            });

        });
    });
</script>
</html>